/**
 * https://leetcode.cn/problems/keyboard-row/
 */
function findWords(words: string[]): string[] {
  const ans: string[] = [];

  const rowIndexs = "12210111011122000010020202";

  for (let word of words) {
    let curIndex =
      rowIndexs[word[0].toLowerCase().charCodeAt(0) - "a".charCodeAt(0)];
    let isSame = true;
    for (let i = 1; i < word.length; i++) {
      if (
        rowIndexs[word[i].toLowerCase().charCodeAt(0) - "a".charCodeAt(0)] !==
        curIndex
      ) {
        isSame = false;
        break;
      }
    }
    if (isSame) {
      ans.push(word);
    }
  }

  return ans;
}

function findWords2(words: string[]): string[] {
  const ans: string[] = [];
  const lines = ["qwertyuiop", "asdfghjkl", "zxcvbnm"];
  const map: Record<string, number> = {};
  lines.forEach((word, index) => {
    for (let w of word) {
      map[w] = index + 1;
    }
  });

  for (let word of words) {
    let line = -1;
    for (let w of word) {
      if (line === -1) {
        // 初始化
        line = map[w.toLowerCase()];
        continue;
      }

      // 说明其他的单词不在同一行
      if (map[w.toLowerCase()] !== line) {
        line = -1;
        break;
      }
    }

    if (line !== -1) {
      ans.push(word);
    }
  }

  return ans;
}
